home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QuickDraw GX / GX->PostScript Sample / GXToPostScript / Imaging Engine / GraphicsState.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.2 KB  |  238 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        GraphicsState.c
  3.  
  4.      Contains:    QuickDraw GX to PostScript conversion code.
  5.                          File contains primitive routines for setting
  6.                         things in the PostScript graphics state that
  7.                         aren't contained in specific files. (Colors
  8.                         and text styles for example are in their own files)
  9.  
  10.      Version:    Technology:    Quickdraw GX 1.1.x
  11.       
  12.      Copyright:    © 1990-1997 by Apple Computer, Inc., all rights reserved.
  13. */
  14.  
  15. #include "GXToPSBuildConfig.h"
  16. #include <GXGraphics.h>
  17. #include "GXGraphicsPriv.h"
  18. #include <GXEnvironment.h>
  19. #include "GXToPostScript.h"
  20. #include "IOUtilities.h"
  21. #include "RDUtil.h"
  22. #include "FontHandler.h"
  23. #include "PublicPostScriptIE.h"
  24. #include "private.h"
  25. #include "PSIEResources.h"
  26. #include "GXErrors.h"
  27. #include "ShapeUtilities.h"
  28.  
  29. #ifdef resumeLabel
  30.     #undef resumeLabel
  31. #endif
  32. #define resumeLabel(exception)
  33.  
  34. /*******************************
  35.  
  36.     MappingPrimitive:
  37.     
  38.     Routine concatonates the mapping with the CTM
  39.     in the current PostScript graphics state.
  40.  
  41.     mapCTM takes the 6 matrix components and the 3,3 component as a scaling.
  42.     
  43. *********************************/
  44. OSErr MappingPrimitive(TIEGlobalsHdl hIEGlobals, gxMapping *theMapping)
  45.     {
  46.         OSErr                            status;
  47.         TRDParams*                pRDParams;
  48.  
  49.         
  50.         pRDParams = (*hIEGlobals)->pRDParams;
  51.         
  52.         pRDParams->resIndex = kMapCTM;
  53.         
  54.         status = RDResPrintf(pRDParams, theMapping->map[0][0], theMapping->map[0][1],
  55.                                                                         theMapping->map[1][0], theMapping->map[1][1],
  56.                                                                         theMapping->map[2][0], theMapping->map[2][1],
  57.                                                                         theMapping->map[2][2]
  58.                                                 );
  59.         ncheck(status);
  60.         return(status);
  61.         
  62.     }//MappingPrimitive
  63.  
  64. //<FF>
  65. /**********************************
  66.  
  67.         DoGsave:
  68.         
  69.         Do a gsave (both in our private graphics state and on the printer)
  70.         First time it is called, it initializes our root level graphics state.
  71.         
  72.         internalOnly:                pass true if we only want to nest out internal state
  73.                                                 without actually outputting a "gsave" operator
  74.         
  75. ************************************/
  76. OSErr _DoGsave(TIEGlobalsHdl hIEGlobals, Boolean internalOnly)
  77.     {
  78.         OSErr                            status;
  79.         TIEGlobalsPtr            pGlobals;
  80.         TRDParams*                pRDParams;
  81.         gxTransform                currTransform;
  82.         gxInk                            currInk;
  83.         gxTag                            currHalftoneTag;
  84.         gxColorProfile        currProfile;
  85.         TGstate                        *pGstate;
  86.         
  87.         pGlobals = *hIEGlobals;
  88.         require (pGlobals->gStateDepth < pGlobals->params.gsaveLimit, failed_TooManyGsaves);
  89.         
  90.         if (!internalOnly) {
  91.         
  92.             /** Output a gsave operator **/
  93.             
  94.             pRDParams = pGlobals->pRDParams;                // grab the RD parameters while we're here.
  95.             
  96.             pRDParams->resIndex = kGsave;                        // Output the gsave operation.
  97.             status = RDResPrintf(pRDParams);
  98.             nrequire(status, failed_Gsave);
  99.     
  100.             pGlobals = *hIEGlobals;                                    // Get it again, I/O could have moved it.
  101.             
  102.         }//end if
  103.  
  104.         
  105.         /****
  106.             gsave on our internal state:
  107.             This means copy the current level into the next level
  108.             and bump up our level index.
  109.             Clone elements so that new level is an ownder of them.
  110.         *****/
  111.         
  112.         BlockMove((Ptr)&(pGlobals->gStateNest[pGlobals->gStateDepth]),
  113.                             (Ptr)&(pGlobals->gStateNest[pGlobals->gStateDepth + 1]),
  114.                             sizeof(TGstate)
  115.                          );        
  116.                          
  117.         pGlobals->gStateDepth += 1;
  118.         
  119.         pGstate = &(pGlobals->gStateNest[pGlobals->gStateDepth]);
  120.  
  121.         pGstate->flags = eNoGstateFlags;
  122.                         
  123.         // Clone the ink and profile and transform and halftone tag for the new level.
  124.         currTransform = pGstate->theTransform;
  125.         currInk = pGstate->theInk;
  126.         currHalftoneTag = pGstate->halftoneTag;
  127.         currProfile = pGstate->currProfile;
  128.  
  129.         GXCloneTransform(currTransform);
  130.         GXCloneInk(currInk);
  131.         
  132.         if (currProfile != nil)
  133.             GXCloneColorProfile(currProfile);
  134.         
  135.         if (currHalftoneTag != nil)
  136.             GXCloneTag(currHalftoneTag);
  137.     
  138.         status = GXGetGraphicsError(nil);
  139.         
  140.             
  141. failed_Gsave:
  142.         
  143.         
  144.         return(status);
  145.         
  146.         
  147. failed_TooManyGsaves:
  148.  
  149.         return(-9999);
  150.     
  151.     }//DoGsave
  152.  
  153.  
  154.  
  155. //<FF>
  156. /**********************************
  157.  
  158.         DoGrestore:
  159.         
  160.         Do a grestore (both in our private graphics state and on the printer)
  161.         This grestore will preserve the style.
  162.         
  163.         internalOnly:            Boolean indicating do do grestore only on internal state
  164.                                                     (that is don't actually output a grestore command to the printer)
  165.         
  166. ************************************/
  167. OSErr _DoGrestore(TIEGlobalsHdl hIEGlobals, Boolean internalOnly)
  168.     {
  169.         OSErr                            status = noErr;
  170.         TIEGlobalsPtr            pGlobals;
  171.         TRDParams*                pRDParams;
  172.         TGstate                        *pGstate;
  173.         
  174.         pGlobals = *hIEGlobals;
  175.         if (pGlobals->gStateDepth >= 0) {    // Grestoring with no gsave just does nothing, just like PostScript®
  176.         
  177.             /*** If we are doing a grestore on the printer: ***/
  178.             if (!internalOnly) {
  179.             
  180.                 /**********
  181.                     If there is a save at this graphics state level,
  182.                         do a restore otherwise the grestore would have no effect
  183.                         because you can't grestore past a save level
  184.                 **********/
  185.                 if (pGlobals->gStateNest[pGlobals->gStateDepth].flags & eGstateDidSave) {
  186.                     
  187.                     pGlobals->gStateNest[pGlobals->gStateDepth].flags &= ~eGstateDidSave;
  188.                     
  189.                     status = RDFlushBuffer(pGlobals->rdMap);        // font handler can do buffer data.
  190.                     nrequire(status, failed_FlushBuffer);
  191.  
  192.                     if ((*hIEGlobals)->fhContext != nil) {            // DL 7/19/97.
  193.                         status = FontHandlerBalanceSaveLevel((*hIEGlobals)->fhContext);
  194.                         nrequire(status, failed_BalanceSave);
  195.                     }//end if
  196.                     
  197.                     /** Because we did a restore, things in the persistent state may be wrong **/
  198.                     
  199.                     (*hIEGlobals)->ieStateFlags |= (eStyleOutOfDate + eFontOutOfDate);
  200.                     
  201.                 }//end if
  202.                 
  203.                 
  204.                 /*** Now output the grestore ***/
  205.                 
  206.                 pRDParams = (*hIEGlobals)->pRDParams;        // grab the RD parameters.
  207.                 pRDParams->resIndex = kQD2Grestore;            // Output the grestore operation.
  208.                 status = RDResPrintf(pRDParams);
  209.                 nrequire(status, failed_Grestore);
  210.                 
  211.             }//end if
  212.     
  213.  
  214.             /*****
  215.                 Do a gsave on our internal state.  This means decrementing the level
  216.                 and disposing of items owned by the level we are restoring from
  217.             *****/
  218.                             
  219.             pGlobals = *hIEGlobals;
  220.             pGstate = &(pGlobals->gStateNest[pGlobals->gStateDepth]);
  221.             
  222.             DisposeGstateItems(pGstate);
  223.             
  224.             // Decrement the current nesting level
  225.             
  226.             --((*hIEGlobals)->gStateDepth);
  227.             
  228.             
  229.         }//end if
  230.         
  231. failed_Grestore:
  232. failed_BalanceSave:    
  233. failed_FlushBuffer:
  234.         
  235.         return(status);
  236.         
  237.     }//DoGrestore
  238.